Updating image entropy implementations#529
Open
fish2000 wants to merge 2 commits intoSmileyChris:masterfrom
Open
Updating image entropy implementations#529fish2000 wants to merge 2 commits intoSmileyChris:masterfrom
fish2000 wants to merge 2 commits intoSmileyChris:masterfrom
Conversation
As of version 6.1.0, the Pillow imaging library’s C module includes an optimized native entropy method: • https://github.com/python-pillow/Pillow/blob/master/CHANGES.rst#610-2019-07-01 This change detects and selects this new native method, if it is available in the version of Pillow upon which `easy-thumbnails` finds itself running. Additionally, an optimized version of the existing Python image-entropy method is furnished as a fallback: rather than summing the entire histogram value set, the function calculates the product of the image dimensions and band count to arrive at the same number. It also uses generator expressions and the specialized `math.fsum(…)` and `math.log2(…)` library calls to improve on the performance of its predecessor without altering the algorithm. The appropriate image-entropy function is then conditionally ensconced in the `easy_thumbnails.utils` module, at module-load time. I also made a slight tweak to the `pil_image` function, found in `easy_thumbnails.source_generators` – the call to `PIL.Image.Image.load()` was raising the exception noted in comments in that functions’ source while I was running the testsuite; now all such calls have these exceptions swallowed and everything works OK.
Mogost
suggested changes
Dec 23, 2019
|
|
||
| # Select the Pillow native image entropy function - if available - | ||
| # and fall back to our Python implementation: | ||
| image_entropy = hasattr(Image.Image, 'entropy') \ |
Contributor
There was a problem hiding this comment.
Please don't break lines with \
| import inspect | ||
| import math | ||
|
|
||
| from django.utils import six |
| hist_size = float(sum(hist)) | ||
| hist = [h / hist_size for h in hist] | ||
| return -sum([p * math.log(p, 2) for p in hist if p != 0]) | ||
| from math import log2, fsum |
|
|
||
| try: | ||
| from PIL import Image, ImageMode | ||
| except ImportError: |
Contributor
There was a problem hiding this comment.
There is non need for this except
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
As of version 6.1.0, the Pillow imaging library’s C module includes an optimized native entropy method:
• https://github.com/python-pillow/Pillow/blob/master/CHANGES.rst#610-2019-07-01
This change detects and selects this new native method, if it is available in the version of Pillow upon which
easy-thumbnailsfinds itself running.Additionally, an optimized version of the existing Python image-entropy method is furnished as a fallback: rather than summing the entire histogram value set, this new function calculates the product of the image dimensions and band count, arriving at the same number. The new function also uses generator expressions and the specialized
math.fsum(…)andmath.log2(…)library calls to improve on the performance of its predecessor without altering the algorithm.The appropriate image-entropy function is then conditionally ensconced in the
easy_thumbnails.utilsmodule, at module-load time.I also made a slight tweak to the
pil_imagefunction, found ineasy_thumbnails.source_generators– the call toPIL.Image.Image.load()was raising the exception noted in comments in that functions’ source while I was running the testsuite; now all such calls have these exceptions swallowed and everything works OK.